<template>
  <div class="app-container">
    <div class="customContainer towSearchHeader">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker style="width:150px" :editable="false" :clearable="false" v-model="pickerDate" value-format="yyyy-MM-dd" type="date" placeholder="截止日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <span style="color: #888;">对接人：</span>
          <el-select v-model="formInline.docking_id" placeholder="请选择对接人" style="width:130px">
            <el-option v-for="item in docking_list" :key="item.id" :label="item.nick" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="supplier_type" style="width:130px" @change="searchChange">
            <el-option v-for="item in search_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-autocomplete clearable v-model="searchVal" style="width:200px" :fetch-suggestions="querySearch" :placeholder="searchPld" :trigger-on-focus="false"
            valueKey="name" size="small" @keyup.enter.native="searchClick"></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="供应商名称" prop="supplier_name"></el-table-column>
        <el-table-column align="center" label="供应商编号" prop="serial_number"></el-table-column>
        <el-table-column align="center" label="总部对接人" prop="clerk_nick"></el-table-column>
        <el-table-column align="center" label="付款类型">
          <template slot-scope="scope">
            <span>{{scope.row.pay_type|payTypeName}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="欠货数量" prop="num" width="100"></el-table-column>
        <el-table-column align="center" label="欠货金额" prop="money" width="100"></el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <router-link :to="{name:'report/supOGItem',params :{id:scope.row.supplier_id,time:formInline.datetime,routerflag:1}}">
              <el-button type="primary" plain size="mini">详情</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <el-table style="margin-top:5px" highlight-current-row :data="totalList" :show-header="false" border size="small">
        <el-table-column align="center" prop="name"></el-table-column>
        <el-table-column align="center" label="欠货数量" prop="all_num" width="100"></el-table-column>
        <el-table-column align="center" label="欠货金额" prop="all_money" width="100"></el-table-column>
        <el-table-column align="center" width="100"></el-table-column>
      </el-table>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber"></el-pagination>
    </div>
  </div>
</template>
<script>
import { getList, getPage } from '@/api/report/supOweGoodsForm';
import { getDocking, getSup } from '@/api/common/commonApi';
import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils';
export default {
  data() {
    return {
      list: null,
      totalList: [{ name: '合计', all_num: '', all_money: '' }],
      listLoading: false,
      search_list: [
        { id: '1', name: '按供应商名称' },
        { id: '2', name: '按供应商编号' }
      ],
      docking_list: [],
      supplier_list: [], //供应商名称列表
      searchPld: '请输入供应商名称',
      searchVal: '',
      supplier_type: '1',
      formInline: {
        datetime: '',
        docking_id: '',
        supplier_no: '',
        supplier_name: ''
      },
      totalDataNumber: 1,
      currentPage: 1,
      pickerDate: new Date().toISOString().split('T')[0],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      }
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.list) {
        vm.getUserList();
      }
    });
  },
  methods: {
    searchChange(val) {
      this.searchVal = '';
      this.searchPld = val == 1 ? '请输入供应商名称' : '请输入供应商编号';
    },
    getUserList() {
      // this.listLoading = true;
      getDocking({}).then(res => {
        this.docking_list = res;
        this.searchClick();
        this.getSupName();
      });
    },
    //获取供应商名称数组
    getSupName() {
      getSup({}).then(res => {
        this.supplier_list = res;
      });
    },
    querySearch(queryString, cb) {
      var supplier_list = this.supplier_list;
      var results = queryString ? supplier_list.filter(this.createFilter(queryString)) : supplier_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return restaurant => {
        if (this.supplier_type == 1) {
          return restaurant.name.indexOf(queryString) >= 0;
        } else {
          return false;
        }
      };
    },
    // 搜索
    searchClick() {
      this.formInline.datetime = this.pickerDate ? this.pickerDate : '';
      if (this.supplier_type == 1) {
        this.formInline.supplier_name = this.searchVal;
        this.formInline.supplier_no = '';
      } else {
        this.formInline.supplier_name = '';
        this.formInline.supplier_no = this.searchVal;
      }
      this.fetchData();
      // optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
      //     this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
      // });
    },
    getData(data) {
      data.forEach(val => {
        val.money = (val.money / 100).toFixed(2);
      });
      this.list = data;
      this.listLoading = false;
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      this.list = [];
      getPage(this.formInline)
        .then(response => {
          if (response.code == 1) {
            let total_data = response.data;
            this.totalList[0].all_money = (total_data.all_money / 100).toFixed(2);
            this.totalList[0].all_num = total_data.all_num;
            this.totalDataNumber = total_data.page * 20;
            if (this.totalDataNumber <= 0) {
              this.listLoading = false;
              return;
            }
            getList(this.formInline).then(res => {
              if (res.code == 1) {
                this.getData(res.data);
              }
            });
          }
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.list = [];
      getList(this.formInline, val).then(res => {
        this.getData(res.data);
        this.listLoading = false;
      });
    }
  }
};
</script>